/*
 * Copyright 2009 Johnson Consulting Services
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.johnsoncs.gwt.widgets.samples.client;

import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DeferredCommand;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TabPanel;
import com.johnsoncs.gwt.application.client.model.Data;
import com.johnsoncs.gwt.widgets.client.dynamictable.TableDefinition.SelectionMode;
import com.johnsoncs.gwt.widgets.client.util.Callback;
import com.johnsoncs.gwt.widgets.samples.client.mock.MockRemoteTableModel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class SamplesEntryPoint implements EntryPoint {

	private void dump(final Data e) {
		dump(e, 0);
		RootPanel.get().add(new Label("***************************************************"));
	}

	private void dump(final Data e, final int indent) {
		String pad = "";
		for (int i = 0; i < indent; i++) {
			pad += "----";
		}
		for (final Data.Property prop : e) {
			if (prop.getValueType().equals(Data.class)) {
				RootPanel.get().add(
						new Label(pad + " Nested: (" + prop.getKeyType().getName() + ")" + prop.getKey().toString()));
				dump((Data) prop.getValue(), indent + 1);
			} else {
				RootPanel.get().add(
						new Label(pad + " (" + prop.getKeyType().getName() + ")" + prop.getKey().toString() + " = ("
						+ prop.getValueType().getName() + ")" + prop.getValue().toString()));
			}
		}
	}

	public void onModuleLoad() {
		Log.setUncaughtExceptionHandler();
		DeferredCommand.addCommand(new Command() {
			@Override
			public void execute() {
				new DbStatusDialog(new Callback<Boolean>() {
					@Override
					public void exec(final Boolean value) {
						if (value) {
							renderDemos();
						} else {
							Window.alert("Unable to initialize database");
						}
					}
				}).check();
			}
		});
	}

	private void renderDemos() {

		final TabPanel tables = new TabPanel();
		tables.add(new SimpleTableDemo(SelectionMode.MULTI_ITEM, true, true), "Multi-select with header/footer");
		tables.add(new SimpleTableDemo(SelectionMode.MULTI_ITEM, true, false), "Multi-select with only header");
		tables.add(new SimpleTableDemo(SelectionMode.SINGLE_ITEM, true, true), "Single-select");
		tables.add(new SimpleTableDemo(SelectionMode.DISABLED, true, true), "Selection disabled");

		tables.addSelectionHandler(new SelectionHandler<Integer>() {
			@Override
			public void onSelection(final SelectionEvent<Integer> tab) {
				((SimpleTableDemo) tables.getWidget(tab.getSelectedItem())).onShow();
			}
		});

		final TabPanel util = new TabPanel();
		util.add(new FocusGroupsDemo(), "Focus Groups");
		util.add(new FadeDemo(), "Fade In/Out");
		util.add(new ListPanelDemo(), "ListPanel");

		final TabPanel demos = new TabPanel();
		demos.add(tables, "Dynamic Table");
		demos.add(new NotificationsDemo(), "Notifications");
		demos.add(util, "Utilities");
		demos.add(new ValidationDemo(), "Validation");
		RootPanel.get().add(demos);
		//testNewSDO();
	}

	private void testNewSDO() {
		final AsyncCallback<Data> callback = new AsyncCallback<Data>() {

			@Override
			public void onFailure(final Throwable caught) {
				throw new RuntimeException(caught);
			}

			@Override
			public void onSuccess(final Data result) {
				try {
					final Data d = result.copy();
					result.set("freshlyadded", "freshlyadded");
					dump(result);
					dump(d);

					Window.alert((String) result.query("child.another"));
					Window.alert("List: " + (result.query("child.list.3")));
					Window.alert((String) result.query("child.list.4.blah"));

				} catch (final Exception e) {
					throw new RuntimeException(e);
				}
			}
		};

		MockRemoteTableModel.Util.getService().getData(callback);

	}
}
